package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class FastDFSUtil {
    /**
     * 加载Tracker链接信息
     */
     static {
        //从classpath下获取文件对象获取路径
        String path = new ClassPathResource("fdfs_client.conf").getPath();
         try {
             ClientGlobal.init(path);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    /**
     * 文件上传
     * @return
     */
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception{
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author",fastDFSFile.getAuthor());

        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        //通过TrackerServer的链接信息可以获取Storage的链接信息，创建StorageClient对象存储Storage的链接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //通过StorageClient访问Storage，实现文件上传，并且获取文件上传后的存储信息
        String[] uploads = storageClient.upload_file(fastDFSFile.getContent(),fastDFSFile.getExt(),meta_list);
        return uploads;
    }
    /**
     * 获取文件信息
     * @return
     */
    public static FileInfo getFile(String groupName, String remoteDileName) throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        //通过TrackerServer的链接信息可以获取Storage的链接信息，创建StorageClient对象存储Storage的链接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        return storageClient.get_file_info(groupName, remoteDileName);
    }

    /**
     * 文件下载
     */
    public static InputStream  downloadFile(String groupName, String remoteDileName) throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        //通过TrackerServer的链接信息可以获取Storage的链接信息，创建StorageClient对象存储Storage的链接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        byte[] bytes = storageClient.download_file(groupName, remoteDileName);
        return new ByteArrayInputStream(bytes);
    }

    /**
     * 删除文件
     */
    public static void deleteFile(String groupName, String remoteDileName) throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        //通过TrackerServer的链接信息可以获取Storage的链接信息，创建StorageClient对象存储Storage的链接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        storageClient.delete_file(groupName, remoteDileName);
    }

    /**
     * 获取Storage信息
     * @return
     */

    public static StorageServer getStorage() throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        //通过TrackerServer的链接信息可以获取Storage的链接信息，创建StorageClient对象存储Storage的链接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        return trackerClient.getStoreStorage(trackerServer);
    }

    /**
     * 获取Storage的IP和端口信息
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteDileName) throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        return trackerClient.getFetchStorages(trackerServer,groupName,remoteDileName);
    }

    /**
     * 获取Tracker信息
     * @return
     * @throws Exception
     */
    public static String getTrackerInfo() throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();

        String ip = trackerServer.getInetSocketAddress().getHostString();
        int tracker_http_port = ClientGlobal.getG_tracker_http_port();
        String url = "http://" + ip + ":" + tracker_http_port;
        return url;
    }

    /**
     * 获取Tracker
     * @return
     * @throws Exception
     */
    public static TrackerServer getTrackerServer() throws Exception{
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient访问TrackerServer服务，获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerServer;
    }

    public static void main(String[] args) throws Exception{
//        FileInfo fileInfo = getFile("group1","M00/00/00/wKjThGBK4YuAV8LTAADorewifTM854.jpg");
//        System.out.println(fileInfo.getSourceIpAddr());
//        System.out.println(fileInfo.getFileSize());

//        //文件下载
//        InputStream is = downloadFile("group1", "M00/00/00/wKjThGBK4YuAV8LTAADorewifTM854.jpg");
//        //将文件写入到本地
//        FileOutputStream os = new FileOutputStream("D:/1.jsp");
//        byte[] buffer = new byte[1024];
//        while(is.read(buffer)!=-1){
//            os.write(buffer);
//        }
//        os.flush();
//        os.close();
//        is.close();

//        //文件删除
//        deleteFile("group1","M00/00/00/wKjThGBK4YuAV8LTAADorewifTM854.jpg");
//        //获取Storage信息
//        StorageServer storageServer = getStorage();
//        System.out.println(storageServer.getStorePathIndex());
//        System.out.println(storageServer.getInetSocketAddress().getHostString());
        //获取Storage组的IP和端口信息
        ServerInfo[] groups = getServerInfo("group1","M00/00/00/wKjThGBK4YuAV8LTAADorewifTM854.jpg");
        for(ServerInfo group : groups){
            System.out.println(group.getIpAddr());
            System.out.println(group.getPort());
        }

    }
}
